﻿using System;
using System.ComponentModel;
using System.Linq;
using ProjectEuler.Linq;

namespace ProjectEuler.Problems
{
    [EulerProblem(21, 31626L)]
    [Description("Evaluate the sum of all amicable pairs under 10000.")]
    internal sealed class Problem021 : EulerProblem
    {
        public override Object Solve()
        {
            var d = Functional.Memoize((Int64 n) => n.ProperDivisors().Sum());

            return (from a in (1L).To(9999L)
                    let b = d(a)
                    where (a != b) && (d(b) == a)
                    select a).Sum();
        }
    }
}
